package com.ldj.common;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.*;
public class pieChartServlet extends HttpServlet {
	public void doPost( HttpServletRequest request,HttpServletResponse response )throws ServletException, IOException{		
		Hashtable rd = (Hashtable)request.getSession().getAttribute("data");
		String tag = request.getParameter("tag");
		if(tag != null && "clear".equals(tag)){
			request.getSession().removeAttribute("data");
		}
		if(rd != null){
			String title = (String)rd.get("pieTitle");
			response.setContentType("image/jpeg");
			DefaultPieDataset dataset = new DefaultPieDataset();
			List list = (List)rd.get("data");
			if(list != null && list.size() > 0){
				for(int i = 0; i < list.size(); i ++){
					Hashtable hd = (Hashtable)list.get(i);
					dataset.setValue((String)hd.get("name"),(Number)hd.get("value"));
				}
			}
			JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true, false, false);
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1, chart, 600, 420, null);
		}
		
	}
	
	public void doGet( HttpServletRequest request,HttpServletResponse response )throws ServletException, IOException{
		this.doPost(request, response);
	}
}
